package cn.qicaiwang.open.qxyApi.mapper;

import cn.qicaiwang.open.qxyApi.entity.ApiInvokeLog;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ApiInvokeLogMapper {

    @Insert("INSERT INTO api_invoke_log(" +
            "trace_id, api_name, api_path, app_key, invoke_time, cost_time, " +
            "request_method, request_headers, request_params, request_body, " +
            "response_code, response_body, success, error_code, error_message, " +
            "exception_stack, client_ip, server_ip, user_agent, create_time, update_time" +
            ") VALUES(" +
            "#{log.traceId}, #{log.apiName}, #{log.apiPath}, #{log.appKey}, #{log.invokeTime}, #{log.costTime}, " +
            "#{log.requestMethod}, #{log.requestHeaders}, #{log.requestParams}, #{log.requestBody}, " +
            "#{log.responseCode}, #{log.responseBody}, #{log.success}, #{log.errorCode}, #{log.errorMessage}, " +
            "#{log.exceptionStack}, #{log.clientIp}, #{log.serverIp}, #{log.userAgent}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "log.id")
    int insert(@Param("log") ApiInvokeLog log);

    @Select("SELECT * FROM api_invoke_log WHERE trace_id = #{traceId}")
    ApiInvokeLog selectByTraceId(@Param("traceId") String traceId);

    @Select("SELECT * FROM api_invoke_log WHERE api_path = #{apiPath} ORDER BY invoke_time DESC LIMIT #{limit}")
    List<ApiInvokeLog> selectRecentByApiPath(@Param("apiPath") String apiPath, @Param("limit") int limit);


    @Select("SELECT * FROM api_invoke_log ORDER BY invoke_time DESC")
    Page<ApiInvokeLog> selectAllPage(Page<ApiInvokeLog> page);
}
